/**
 * 更新股票指数
 * @param ctx
 * @returns {Promise<void>}
 */
exports.updateStockIndex = async function (ctx) {
  const query = ctx.request.body
  try {
    const data = ctx.validateData({
      key: { required: true, type: 'string' },
      year_average: { required: false, type: 'number' },
      flag: { required: false, type: 'string' },
      status: { required: false, type: 'string' },
      no_sell_status: { required: false, type: 'string' },
      cut_down: { required: false, type: 'string' },
      top_close: { required: false, type: 'number' },
      target_up_close: { required: false, type: 'number' },
      target_down_close: { required: false, type: 'number' },
      danger: { required: false, type: 'string' },
      z45: { required: false, type: 'string' },
      recent_status: { required: false, type: 'string' },
      one_deep: { required: false, type: 'string' }
    }, query)
    await ctx.services.stockIndex.updateStockIndex(data)
    ctx.body = ctx.resuccess()
  } catch (err) {
    ctx.body = ctx.refail(err)
  }
}

/**
 * 查询股票指数
 * @param ctx
 * @returns {Promise<void>}
 */
exports.getStockIndexAll = async function (ctx) {
  try {
    const res = await ctx.services.stockIndex.getStockIndexAll()
    ctx.body = ctx.resuccess(res)
  } catch (err) {
    ctx.body = ctx.refail(err)
  }
}

/**
 * 查询单个股票指数
 * @param ctx
 * @returns {Promise<void>}
 */
exports.getStockIndex = async function (ctx) {
  const query = ctx.query
  try {
    const data = ctx.validateData({
      code: { type: 'string', required: true }
    }, query)
    const res = await ctx.services.stockIndex.getStockIndex(data)
    ctx.body = ctx.resuccess(res)
  } catch (err) {
    ctx.body = ctx.refail(err)
  }
}

/**
 * 更新年线
 * @param ctx
 * @returns {Promise<void>}
 */
exports.updateStockIndexYearAverage = async function (ctx) {
  try {
    await ctx.services.stockIndex.updateStockIndexYearAverage()
    ctx.body = ctx.resuccess()
  } catch (err) {
    ctx.body = ctx.refail(err)
  }
}
